package logic

import (
	"context"
	"fmt"

	"probe-scheduler/internal/svc"
	pb "probe-scheduler/pb/scheduler"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResumeTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResumeTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResumeTaskLogic {
	return &ResumeTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResumeTaskLogic) ResumeTask(in *pb.ResumeTaskRequest) (*pb.ResumeTaskResponse, error) {
	if in.TaskId == 0 {
		return &pb.ResumeTaskResponse{
			Base: &pb.CommonResponse{
				Success: false,
				Message: "task id is required",
			},
		}, nil
	}

	if err := l.svcCtx.TaskDao.ResumeTask(l.ctx, in.TaskId); err != nil {
		return &pb.ResumeTaskResponse{
			Base: &pb.CommonResponse{Success: false, Message: fmt.Sprintf("恢复失败: %v", err)},
		}, nil
	}

	err := l.svcCtx.Scheduler.ResumeTask(l.ctx, in.TaskId)
	if err != nil {
		l.Errorf("failed to resume task %s: %v", in.TaskId, err)
		return &pb.ResumeTaskResponse{
			Base: &pb.CommonResponse{
				Success: false,
				Message: fmt.Sprintf("failed to resume task: %v", err),
			},
		}, nil
	}

	return &pb.ResumeTaskResponse{
		Base: &pb.CommonResponse{
			Success: true,
			Message: "task resumed successfully",
		},
	}, nil
}
